package gui.teamEditor;

import static org.junit.Assert.*;

import java.util.HashSet;
import java.util.Set;

import gui.teamEditor.PlayerPanel;

import org.junit.Before;
import org.junit.Test;

import data.*;
import data.Data.*;

/**
 * Tests basic save/load between the player and its player panel.
 * @author Julien
 *
 */
public class PlayerPanelTest {
	Team team = null;
	Player player = null;
	PlayerPanel panel = null;

	@Before
	public void setUp() throws Exception {
		team = new Team("Test team", Race.ELF);
		team.resetToDefault();
		player = team.players().get(0);
		panel = new PlayerPanel(player, team);
	}

	@Test
	public void testPlayerPanel() {
		// Test post-construction values in the dialog.
		testPanelMatchedPlayer();
	}

	@Test
	public void testGetPlayer() {
		assertTrue( panel.getPlayer() == player );
	}

	@Test
	public void testCommit() {
		// Change dialog values
		panel.m_name.setText("Not previous name");
		panel.m_number.setText("99");
		panel.m_level.setText("10");
		panel.m_experience.setText("442");
		panel.m_MA.setText("9");
		panel.m_ST.setText("8");
		panel.m_AG.setText("7");
		panel.m_AV.setText("6");
		
		int skillIndices[] = new int[] { 0, 3, 4 };
		panel.m_skills.setSelectedIndices(skillIndices);
		
		// And then commit
		panel.commit();
		
		// Check if everything matches correctly
		testPanelMatchedPlayer();
	}
	
	public void testPanelMatchedPlayer() {
		assertTrue( player.name().equals( panel.m_name.getText() ) );
		assertTrue( player.number() == Integer.parseInt( panel.m_number.getText() ) );
		assertTrue( player.level() == Integer.parseInt( panel.m_level.getText() ) );
		assertTrue( player.XP() == Integer.parseInt( panel.m_experience.getText() ) );
		assertTrue( player.MA() == Integer.parseInt( panel.m_MA.getText() ) );
		assertTrue( player.ST() == Integer.parseInt( panel.m_ST.getText() ) );
		assertTrue( player.AG() == Integer.parseInt( panel.m_AG.getText() ) );
		assertTrue( player.AV() == Integer.parseInt( panel.m_AV.getText() ) );
	
		Set< Skill > skillSet = new HashSet< Skill >();
		Object[] panelSkills = panel.m_skills.getSelectedValues();
		for( Object skill : panelSkills )
			skillSet.add( (Skill) skill );

		assertTrue(player.skills().equals( skillSet ) );
	}
}
